//
//  EnterPhoneNumViewController.swift
//  Nahuo
//
//  Created by zang qilong on 2017/5/13.
//  Copyright © 2017年 zang qilong. All rights reserved.
//

import UIKit
import SwiftyJSON
import URLNavigator
import MBProgressHUD

class EnterPhoneNumViewController: UIViewController {

    
    @IBOutlet weak var phoneTextField: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    // MARK: - IBAction
    
    @IBAction func verifyPhone() {
        guard let phoneNum = phoneTextField.text, !phoneNum.isEmpty else {
            print("phoneNum is nil");
            return
        }
        
        let hud = MBProgressHUD.showAdded(to: self.view, animated: true)
        
        let verifyPhoneRequset = VerifyPhoneRequest(mobile: phoneNum)
        verifyPhoneRequset.startWithCompletionBlock(success: { (request) in
            hud.hide(animated: true)
            let json = JSON(request.responseObject!)
            if let code = json["code"].int {
                
                if code == 0 {
                    let vc = UIStoryboard.init(name: "Login", bundle: nil).instantiateViewController(withIdentifier: "kVerifyCodeVC") as! VerifyCodeViewController
                    vc.phoneNum = phoneNum
                    self.navigationController?.pushViewController(vc, animated: true)
                }else if code == 1 {
                    let vc = UIStoryboard.init(name: "Login", bundle: nil).instantiateViewController(withIdentifier: "kEnterPasswordVC") as! EnterPasswordViewController
                    vc.phoneNum = phoneNum
                    self.navigationController?.pushViewController(vc, animated: true)
                }else {
                    if let message = json["message"].string {
                        MBProgressHUD.showTips(tip: message, view: self.view)
                    }
                }
            }else {
                
            }
        }) { (request) in
            hud.hide(animated: true)
        }
       
    }

}
